package com.sita.tboard.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.sita.bike.R;
import com.sita.bike.utils.BitmapUtils;
import com.sita.bike.utils.LogUtils;


/**
 * Created by LiJuan on 15/10/20.
 */
public class BigDial extends View {
    private final String TAG = BigDial.class.getSimpleName();

    private Bitmap backBitmap, runBitmap, pointerBitmap, all;
    private Paint mBackPaint, mRunPaint, mTextPaint;
    private RectF mOval;
    private float mAngle;
    int widthMeasureSpec, heightMeasureSpec;
    private String speed = "0";

    public BigDial(Context context) {
        super(context);
        init();

    }

    public BigDial(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        backBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_background, options);
        runBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_run, options);
        pointerBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_pointer, options);
        mBackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//消除锯齿
        mRunPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(100);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(0xffffaa00);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Shader shader = new BitmapShader(runBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//CLAMP 拉伸
        mBackPaint.setShader(shader);
        mOval = new RectF(0, 0, runBitmap.getWidth(), runBitmap.getHeight());
        LogUtils.i(TAG,"onSizeChanged " +  "w="+ w + ", h=" + h );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int newWidth = getWidth();
        int newHeight = getHeight();
        LogUtils.i(TAG, "onDraw " + "w="+ newWidth + ", h=" + newHeight );

//        float scale = (float) newHeight / (float) backBitmap.getHeight();
//
//        backBitmap = BitmapUtils.scaleBitmap(backBitmap, scale);
//        runBitmap = BitmapUtils.scaleBitmap(runBitmap, scale);
//        pointerBitmap = BitmapUtils.scaleBitmap(pointerBitmap, scale);
//        Shader shader = new BitmapShader(runBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//CLAMP 拉伸
//        mBackPaint.setShader(shader);
//        mOval = new RectF(0, 0, runBitmap.getWidth(), runBitmap.getHeight());


        // old code
        RectF dst = new RectF(0, 0, backBitmap.getWidth(), backBitmap.getHeight());
        canvas.drawBitmap(backBitmap, null, dst, mRunPaint);
        canvas.drawArc(mOval, 0, (float) 1.6 * mAngle + 50, true, mBackPaint);
        canvas.drawText(speed, backBitmap.getWidth() / 2, backBitmap.getHeight() / 2 + 40, mTextPaint);
        canvas.save();
        canvas.rotate((float) 1.6 * mAngle, pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
        canvas.drawBitmap(pointerBitmap, null, dst, mRunPaint);
        canvas.restore();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = runBitmap.getHeight() ;
        int measuredWidth = runBitmap.getWidth();
        setMeasuredDimension( measuredWidth, measuredHeight);

        //
//        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
//        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//        int viewWidthHeight = widthSize > heightSize ? heightSize : widthSize;
//        setMeasuredDimension(viewWidthHeight,viewWidthHeight);
//        LogUtils.i(TAG, "onMeasure " + "w="+ viewWidthHeight + ", h=" + viewWidthHeight );
    }

    public void setAngle(float angle) {
        this.mAngle = angle;
        this.speed = String.valueOf((int) angle);
        invalidate();
    }


}